{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**884. Uncommon Words from Two Sentences**  <font color=#green size=3>_easy_</font>\n",
    "\n",
    "We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word consists only of lowercase letters.)\n",
    "\n",
    "A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.\n",
    "\n",
    "Return a list of all uncommon words. \n",
    "\n",
    "You may return the list in any order.\n",
    "\n",
    "**Example 1**\n",
    "\n",
    "```\n",
    "Input: A = \"this apple is sweet\", B = \"this apple is sour\"\n",
    "Output: [\"sweet\",\"sour\"]\n",
    "```\n",
    "\n",
    "**Example 2**\n",
    "\n",
    "```\n",
    "Input: A = \"apple apple\", B = \"banana\"\n",
    "Output: [\"banana\"]\n",
    "```\n",
    "\n",
    "**Note:**\n",
    "- 0 <= A.length <= 200\n",
    "- 0 <= B.length <= 200\n",
    "- A and B both contain only spaces and lowercase letters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['sweet', 'sour']\n",
      "['banana']\n"
     ]
    }
   ],
   "source": [
    "# def tohash(A):\n",
    "#     hash = {}\n",
    "#     for w in A.split(' '):\n",
    "#         if hash.get(w) is None:\n",
    "#             hash[w] = 1\n",
    "#         else: hash[w] += 1\n",
    "#     return hash\n",
    "\n",
    "def uncommonFromSentences(A, B):\n",
    "    \"\"\"\n",
    "    :type A: str\n",
    "    :type B: str\n",
    "    :rtype: List[str]\n",
    "    \"\"\"\n",
    "    # res = []\n",
    "    # total_hash = tohash(A + ' ' + B)\n",
    "    # for key in total_hash.keys():\n",
    "    #     if total_hash[key] == 1:\n",
    "    #         res.append(key)\n",
    "    # return res\n",
    "    total_hash = {}\n",
    "    for w in A.split():\n",
    "        total_hash[w] = total_hash.get(w, 0) + 1\n",
    "    for w in B.split():\n",
    "        total_hash[w] = total_hash.get(w, 0) + 1\n",
    "\n",
    "    return [w for w in total_hash if total_hash[w] == 1]\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    print(uncommonFromSentences(\"this apple is sweet\",\"this apple is sour\"))\n",
    "    print(uncommonFromSentences(\"apple apple\", \"banana\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
